// pages/indexAll/yiqiStep1/yiqiStep1.js
const app = getApp()
var self;
var id, loginid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    currentTab: 0,
    yestime: [],
    notime: [],
    mintime: "",
    maxtime: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    id = options.id;
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    month = (month < 10 ? "0" + month : month);
    day = (day < 10 ? "0" + day : day);
    var nowDate = year + "-" + month + "-" + day;
    self.setData({
      date: nowDate
    })
    loginid = app.checkLogin();
    
  },

  selectTime: function (e) {
    var selectindex = e.currentTarget.dataset.index;
    var yestime = this.data.yestime;
    for(var i=0;i<yestime.length;i++){
      if(selectindex==i){
        yestime[i].check = true;
      }else{
        yestime[i].check = false;
      }
    }
    
    self.setData({
      yestime: yestime
    });
    self.setYuyueTime();
  },
  setYuyueTime: function () {
    var yestime = this.data.yestime;
    var mintime = "";
    var maxtime = "";
    for (var i = 0; i < yestime.length; i++) {
      if (yestime[i].check) {
        if (mintime == "") {
          mintime = yestime[i].starttime;
        }
        maxtime = yestime[i].endtime;
      }
    }
    self.setData({
      mintime: mintime,
      maxtime: maxtime
    })
  },
  getAllTime: function () {
    loginid = wx.getStorageSync('user') == "" ? -999 : wx.getStorageSync('user').UserID;
    if (loginid == -999) {
      return false;
    }
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetKeYueAndYiYueShiJian",
      data: {
        YiQiID: id,
        LoginID: loginid,
        XuanZeRiQi: self.data.date
      },
      method: 'GET',
      success: function (res) {
        var yestime = [];
        var notime = [];
        var tmptime = res.data.retData.KeYueShiJian;
        for (var i = 0; i < tmptime.length; i++) {
          var obj = {};
          obj.starttime = tmptime[i].KaiShiShiJian;
          obj.endtime = tmptime[i].JieShuShiJian;
          obj.content = tmptime[i].KaiShiShiJian + "-" + tmptime[i].JieShuShiJian;
          obj.check = false;
          yestime.push(obj);
        }
        var tmptime = res.data.retData.YiYueShiJian;
        for (var i = 0; i < tmptime.length; i++) {
          var obj = {};
          obj.starttime = tmptime[i].KaiShiShiJian;
          obj.endtime = tmptime[i].JieShuShiJian;
          obj.content = tmptime[i].KaiShiShiJian + "-" + tmptime[i].JieShuShiJian;
          obj.name = tmptime[i].XingMing;
          notime.push(obj);
        }
        self.setData({
          yestime: yestime,
          notime: notime
        })

      }
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
    this.getAllTime()
  },
  // 滑动切换tab 
  bindChange: function (e) {

    this.setData({
      currentTab: e.detail.current
    });
  },
  // 点击tab切换 
  swichNav: function (e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  goNext: function (e) {
    loginid = app.checkLogin();
    if(!loginid){
      return false;
    }
    if (this.data.mintime == "") {
      wx.showToast({
        title: '请选择预约时间',
        icon: 'none'
      })
      return false;
    }


    //开始组装并校验数据
    var SongYangShiJian = this.data.date + " " + this.data.mintime;
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=JudgeYuYueDate",
      data: {
        YiQiID: id,
        LoginID: loginid,
        SongYangShiJian: SongYangShiJian,
        YuYueMoShi: 1
      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {              //测试为0 
          var obj = {};
          obj.mindate = self.data.date;
          obj.maxdate = self.data.date;
          obj.mintime = self.data.mintime;
          obj.maxtime = self.data.maxtime;

          wx.navigateTo({
            url: '/pages/indexAll/songyangStep2/songyangStep2?tmpdata=' + JSON.stringify(obj) + "&id=" + id + "&loginid=" + loginid,
          })
        } else if (res.data.state == -1) {
          app.goLogin();
        } else {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }
      }
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.getAllTime();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})